package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.DownlineIncomeRecord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 下级收益详情适配器
 */
public class DownlineIncomeAdapter extends RecyclerView.Adapter<DownlineIncomeAdapter.ViewHolder> {

    private Context context;
    private List<DownlineIncomeRecord> records;
    private SimpleDateFormat inputFormat;
    private SimpleDateFormat outputFormat;

    public DownlineIncomeAdapter(Context context, List<DownlineIncomeRecord> records) {
        this.context = context;
        this.records = records;
        this.inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        this.outputFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_downline_income, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DownlineIncomeRecord record = records.get(position);
        
        // 设置下级用户昵称
        holder.tvDownlineNickname.setText(record.getDownlineNickname());
        
        // 设置下级等级标签
        holder.tvLevel.setText(record.getLevelText());
        int textColorRes = record.getLevel() == 1 ? R.color.colorWarning : R.color.colorInfo;
        holder.tvLevel.setTextColor(ContextCompat.getColor(context, textColorRes));
        
        // 设置任务名称
        holder.tvTaskName.setText(record.getDescription());
        
        // 设置收益金额
        holder.tvAmount.setText(String.format("+¥%.2f", record.getAmount()));
        
        // 格式化并设置时间
        try {
            Date date = inputFormat.parse(record.getCreateTime());
            if (date != null) {
                holder.tvCreateTime.setText(outputFormat.format(date));
            } else {
                holder.tvCreateTime.setText(record.getCreateTime());
            }
        } catch (ParseException e) {
            holder.tvCreateTime.setText(record.getCreateTime());
        }
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDownlineNickname;
        TextView tvLevel;
        TextView tvTaskName;
        TextView tvAmount;
        TextView tvCreateTime;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDownlineNickname = itemView.findViewById(R.id.tv_downline_nickname);
            tvLevel = itemView.findViewById(R.id.tv_level);
            tvTaskName = itemView.findViewById(R.id.tv_task_name);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvCreateTime = itemView.findViewById(R.id.tv_create_time);
        }
    }
} 